
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using RSSRadio.Model;

namespace RSSRadio
{
	public static class PodcastActions
	{
		/// <summary>
		/// Plays a podcast using the default associated application
		/// </summary>
		/// <param name="fileName"></param>
		public static void Play(string fileName)
		{
			if (fileName != null && File.Exists(fileName))
				System.Diagnostics.Process.Start(fileName, null);
			else
				MessageBox.Show("File not found", "Error");
		}

		/// <summary>
		/// Removes a podcast from the list of downloaded podcasts
		/// </summary>
		/// <param name="podcast"></param>
		private static void Delete(PodcastInfo podcast)
		{
			// delete the media file
			try
			{
				if (File.Exists(podcast.FilePath))
					File.Delete(podcast.FilePath);
			}
			catch
			{
				try
				{
					WinApi.CloseMediaPlayer();
					Thread.Sleep(1000);

					if (File.Exists(podcast.FilePath))
						File.Delete(podcast.FilePath);
				}
				catch
				{
					MessageBox.Show("Error deleting file.");
					return;
				}
			}

			// remove from xml and save it
			Globals.DownloadedPodcasts.RemoveItem(podcast);
			Globals.DownloadedPodcasts.SaveToXml(Globals.AppSettings.DownloadedPodcastListXmlLoc);
		}

        public static bool canDownloadPodcast(Subscription feed, EpisodeDetails episode)
        {
            bool canDownload = true;
            if ( !Globals.DownloadedPodcasts.Contains(episode))
            {
                canDownload = !IsEpisodeInDownloadQueue(episode);
            }
            else
            {
                canDownload = false;
            }
            return canDownload;
        }

        public static bool IsEpisodeInDownloadQueue(EpisodeDetails episode)
        {
            bool isEpisodeInDownloadQueue = false;
            foreach (PodcastDownloader2 downloader in Globals.ActivePodcastDownloaders)
            {
                if (downloader.episode.Url.ToString() == episode.podcastUrl.ToString())
                {
                    isEpisodeInDownloadQueue = true;
                    break;
                }
            }
            return isEpisodeInDownloadQueue;
        }


        /// <summary>
        /// Attempts to download a feed episode (podcast). IF the episode is already downloaded or download is in porgress method returns without an error
        /// </summary>
        /// <param name="feed"></param>
        /// <param name="episode"></param>
        public static void Download(Subscription feed, EpisodeDetails episode)
        {
            if (canDownloadPodcast(feed, episode))
            {
                try
                {
                    PodcastDownloader2 downloader = new PodcastDownloader2(feed, episode);
                    downloader.DownloadAsync();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot download this podcast:" + ex.Message);
                }
            }
        }

	}
}
